import asyncio
import websockets
import time
import json

async def get_okx_btc_ticker():
    url = "wss://ws.okx.com:8443/ws/v5/public"
    subscribe_message = {
        "op": "subscribe",
        "args": [{"channel": "tickers", "instId": "BTC-USDT"}]
    }

    async with websockets.connect(url) as websocket:
        await websocket.send(json.dumps(subscribe_message))
        print("Subscribed to BTC-USDT ticker channel")

        while True:
            response = await websocket.recv()
            data = json.loads(response)

            if 'data' in data:
                # Extracting timestamp from the message
                tick = data['data'][0]
                exchange_time = int(tick['ts']) / 1000  # OKX provides timestamp in milliseconds
                local_time = time.time()

                # Calculate delay
                delay = local_time - exchange_time
                print(f"Exchange time: {exchange_time}, Local time: {local_time}, Delay: {delay:.6f} seconds")

async def main():
    await get_okx_btc_ticker()

if __name__ == "__main__":
    asyncio.run(main())
